package com.pig4cloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.mall.product.dto.BrandParam;
import com.pig4cloud.mall.product.entity.Brand;
import com.pig4cloud.mall.product.mapper.BrandMapper;
import com.pig4cloud.mall.product.service.BrandService;
import com.pig4cloud.pig.common.core.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 品牌表
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

	@Resource
	private BrandMapper brandMapper;

	@Override
	public int create(BrandParam brandParam) {
		Brand brand = new Brand();
		BeanUtils.copyProperties(brandParam, brand);
		// 如果创建时首字母为空，取名称的第一个为首字母
		if (StringUtils.isEmpty(brand.getFirstLetter())) {
			brand.setFirstLetter(brand.getName().substring(0, 1));
		}
		return brandMapper.insert(brand);
	}

	@Override
	public List<Brand> listAllBrand() {
		return brandMapper.listAllBrand();
	}

	@Override
	public PageResult listBrand(String keyword, Integer page, Integer pageSize) {
		IPage<Brand> iPage;
		if (StringUtils.isEmpty(keyword)) {
			iPage = brandMapper.selectPage(new Page<>(page, pageSize), new QueryWrapper<>());
		} else {
			QueryWrapper<Brand> wrapper = new QueryWrapper<Brand>().eq("name", keyword);
			wrapper.orderByDesc("sort");
			iPage = brandMapper.selectPage(new Page<>(page, pageSize), wrapper);
		}
		return PageResult.buildPage(iPage);
	}

	@Override
	public int update(BrandParam brandParam, Long id) {
		Brand brand = new Brand();
		BeanUtils.copyProperties(brandParam, brand);
		brand.setId(id);
		//如果创建时首字母为空，取名称的第一个为首字母
		if (StringUtils.isEmpty(brand.getFirstLetter())) {
			brand.setFirstLetter(brand.getName().substring(0, 1));
		}
		// TODO 更新品牌时要更新商品中的品牌名称
		return brandMapper.updateById(brand);
	}

	@Override
	public Brand getBrand(Long id) {
		return brandMapper.selectById(id);
	}

	@Override
	public int delete(List<Long> ids) {
		return brandMapper.deleteBatchIds(ids);
	}

	@Override
	public int delete(Long id) {
		return brandMapper.deleteById(id);
	}

	@Override
	public boolean updateShowStatus(List<Long> ids, Integer showStatus) {
		List<Brand> brandList = new ArrayList<>();
		for (Long id : ids) {
			Brand brand = new Brand();
			brand.setId(id);
			brand.setShowStatus(showStatus);
			brandList.add(brand);
		}
		return this.updateBatchById(brandList);
	}

	@Override
	public boolean updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
		List<Brand> brandList = new ArrayList<>();
		for (Long id : ids) {
			Brand brand = new Brand();
			brand.setId(id);
			brand.setFactoryStatus(factoryStatus);
			brandList.add(brand);
		}
		return this.updateBatchById(brandList);
	}
}
